<template>
  <div class="login">
    <img :src="imgSrc" width="100%" height="100%" alt=""/>
    <div class="loginPart">
      <h2>用户登录</h2>
      <el-form class="loginForm">
        <div class="inputElement">
          <el-input v-model="loginParams.username" prefix-icon="el-icon-user" placeholder="请输入用户名/账号"></el-input>
        </div>
        <div class="inputElement">
          <el-input v-model="loginParams.password" prefix-icon="el-icon-lock" placeholder="请输入密码"
                    @keyup.enter.native="login" show-password></el-input>
        </div>
        <div class="loginButton">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import encoder from '../../utils/EncodingUtil'
import CacheUtil from "@/utils/CacheUtil";

export default {
  name: "Login",
  data() {
    return {
      imgSrc: require('@/assets/photos/loginBackground.webp'),
      loginParams: {
        username: '',
        password: ''
      },
    }
  },
  methods: {
    getUserInfo() {
      let userinfo = ""
      this.$axios.get(
          `${this.HOST}/userinfo`,
      ).then(res => {
        const info = res.data
        if (info.code === 200) {
          CacheUtil.cacheUserInfo(JSON.parse(info.data))
        }
        console.log("beforeR", CacheUtil.getUserInfo());
        if (this.$route.query.goindex === 'true') {
          this.$router.push('/')
        } else {
          this.$router.back(-1)
        }
      })
    },
    async login() {
      this.$axios.post(
          `${this.HOST}/login`,
          {
            username: this.loginParams.username,
            password: encoder.hex_sha1(`${this.loginParams.password}`)
          }
      ).then(res => {
        const info = res.data
        if (info.code === 200) {
          window.sessionStorage.setItem('access_token', info.data.token.token)
          let user = info.data
          CacheUtil.cacheUser(user)
          this.$message({
            message: info.message,
            type: 'success',
            offset: 90,
            duration: 1000
          })
          this.getUserInfo()
        }
      })
    }
  }
}
</script>

<style scoped>
.loginPart {
  position: absolute;
  /*定位方式绝对定位absolute*/
  top: 50%;
  left: 50%;
  /*顶和高同时设置50%实现的是同时水平垂直居中效果*/
  transform: translate(-50%, -50%);
  /*实现块元素百分比下居中*/
  width: 450px;
  padding: 50px;
  background: rgba(0, 0, 0, .5);
  /*背景颜色为黑色，透明度为0.8*/
  box-sizing: border-box;
  /*box-sizing设置盒子模型的解析模式为怪异盒模型，
  将border和padding划归到width范围内*/
  box-shadow: 0px 15px 25px rgba(0, 0, 0, .5);
  /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
  border-radius: 15px;
  /*边框圆角，四个角均为15px*/
}

.loginPart h2 {
  margin: 0 0 30px;
  padding: 0;
  color: #fff;
  text-align: center;
  /*文字居中*/
}

.loginForm >>> .el-input__inner {
  width: 90%;
  padding: 10px 0;
  font-size: 16px;
  color: #fff;
  letter-spacing: 1px;
  /*字符间的间距1px*/
  margin-top: 10px;
  margin-left: 30px;
  margin-bottom: 30px;
  border: none;
  border-bottom: 1px solid #fff;
  outline: none;
  /*outline用于绘制元素周围的线
  outline：none在这里用途是将输入框的边框的线条使其消失*/
  background: transparent;
  /*背景颜色为透明*/
}

.login {
  width: 100%;
  height: 100%;
}

.loginButton {
  position: absolute;
  top: 82%;
  left: 50%;
  margin-left: -34px;
}

.loginForm >>> .el-input__icon {
  font-size: 18px;
  height: 80%;
  margin-left: -30px;
}
</style>
